<template>
	<view style="background-color: #F1F1F1;">
		<view class="products">
			<view class="title">
				——&nbsp;&nbsp;产品展示&nbsp;&nbsp;——
			</view>
			<view class="content">
				<block v-for="(v, index) in product.list" :key="index">
					<view class="block" @tap="goDetail(v)">
						<image class="img" :src="v.img_url"></image>
						{{v.name}}
					</view>
				</block>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				product: {
					pageNumber: 1,
					firstPage: true,
					lastPage: true,
					list: [],
				},
			}
		},
		onReachBottom(){
			console.log('触底');
			if(this.page.lastPage) {this.noMore = true;return;}
			this.getProductMore(++this.page.pageNumber);
		},
		methods: {
			getProduct(pageNumber){
				let self = this;
				uni.showLoading({title: '加载中',mask: true});
				this.config.post('/api/product/findPage', {
					pageNum: pageNumber?pageNumber:1,
				}).then(res=>{
					uni.hideLoading();
					if(res.data.code==='0'){
						self.product.pageNumber = res.data.page.pageNumber;
						self.product.firstPage = res.data.page.firstPage;
						self.product.lastPage = res.data.page.lastPage;
						self.product.list = res.data.page.list;
					}else{
						uni.showToast({title: self.config.handleError(res.data),icon: 'none'});
					}
				})
			},
			getProductMore(pageNumber){
				let self = this;
				uni.showLoading({title: '加载中',mask: true});
				this.config.post('/api/product/findPage', {
					pageNum: pageNumber?pageNumber:1,
				}).then(res=>{
					uni.hideLoading();
					if(res.data.code==='0'){
						self.product.pageNumber = res.data.page.pageNumber;
						self.product.firstPage = res.data.page.firstPage;
						self.product.lastPage = res.data.page.lastPage;
						self.product.list = self.product.list.concat(res.data.page.list);
					}else{
						uni.showToast({title: self.config.handleError(res.data),icon: 'none'});
					}
				})
			},
			goDetail(v){
				uni.navigateTo({
					url: '/pages/main/make-order/make-order?id='+v.id
				})
			},
		},
		onShow() {
			this.getProduct('1');
		},
	}
</script>

<style scoped lang="less">
	.products{
		width: 100%;
		margin-top: 10upx;
		background-color: #fff;
		text-align: center;
		.title{
			width: 100%;
			font-size: 25upx;
			font-weight: bold;
			color: #EA463C;
			padding-top: 20upx;
			margin-bottom: 20upx;
		}
		.content{
			width: 100%;
			display: inline-flex;
			flex-wrap: wrap;
			margin-bottom: 10upx;
			.block{
				width: 46%;
				height: 250upx;
				box-shadow: 0 0 10px 0 #ccc;
				border-radius: 8upx;
				font-size: 25upx;
				margin: 0 2% 15upx 2%;
				.img{
					width: 100%;
					height: 200upx;
					border-top-left-radius: 8upx;
					border-top-right-radius: 8upx;
				}
			}
		}
	}
</style>
